

jQuery(function ($) {
    var ask_id = window.location.search.split('=')[1];
    var pageOnoff = true;
    var onoff = false;
    generateList(0);
    function generateList(page) {
        var asks_id = [];
        var param = {
            "KeywordsTags": {
                "id": ask_id
            },
            "AskKeywords[]": {
                "query": "2",
                "count": 5,
                "page": page,
                "AskKeywords": {
                    "keywords_id@": "KeywordsTags/id",
                    "Ask": {
                        "id@": "/ask_id"
                    }
                }
            },
            "total@": "/AskKeywords[]/total"
        };
        send_ajax('post', '/get', param, function callback(data) {
            if (data.code == 200 && data['AskKeywords[]']) {
                //评价标签渲染
                var tag = function tag(id) {
                    var param = {
                        "AskKeywords[]": {
                            "AskKeywords": {
                                "ask_id": id,
                                "KeywordsTags": {
                                    "id@": "/keywords_id",
                                    "type": "2"
                                }
                            }
                        }
                    };
                    send_ajax('post', '/get', param, function callback(data) {
                        if (data.code == 200 && data["AskKeywords[]"]) {
                            for (var i = 0; i < data["AskKeywords[]"].length; i++) {
                                asks_id.push(data["AskKeywords[]"][i]);
                            }
                        }
                    });
                };

                var KeywordsTags = data.KeywordsTags;
                $('.keyWord').html(KeywordsTags.title);
                $('.blue').html(KeywordsTags.title);
                if(pageOnoff){
                    twbsPaginations(data.total);
                    pageOnoff = false;
                }
                var data = data['AskKeywords[]'];
                var html = "";
                //分页按钮
                for (var i = 0; i < data.length; i++) {
                    tag(data[i].Ask.id);
                    html += " <li>\n                        <div class=\"main_msg\">\n                            <p class=\"need_rep\"><span>\u95EE:</span> " + data[i].Ask.title + "</p>\n                            <p class=\"need_rep\"><span>\u7B54:</span>" + data[i].Ask.content + "\n                            </p>\n                            <p class=\"clearfix\">\n                                <span class=\"ask_tag_tip\" data-ask-id=" + data[i].Ask.id + ">\n                                   \n                                </span>\n                                <span class=\"ask_time\">\n                                " + data[i].Ask.update_time.substring(0, 11) + "\n                                </span>\n                                <span class=\"reply_num\">\n                                    <em>0</em>\n                                    \u56DE\u7B54\n                                </span>\n                            </p>\n                        </div>\n                    </li> ";
                };
                $('.q_list_bd').html(html);
                var div = document.querySelector(".q_list_bd");
                var re = new RegExp(KeywordsTags.title, "g");
                div.innerHTML = div.innerHTML.replace(re, "<font color=#FF0000>" + KeywordsTags.title + "</font>");
                var tag_el = $('.q_list_bd .ask_tag_tip');;
                for (var n = 0; n < tag_el.length; n++) {
                    for (var j = 0; j < asks_id.length; j++) {
                        if ($(tag_el[n]).attr('data-ask-id') == asks_id[j].ask_id) {
                            $(tag_el[n]).append('<a href="/question/question_ask_tag.html?tag_id=' + asks_id[j].id + '" target="_blank">' + (asks_id[j].KeywordsTags ? asks_id[j].KeywordsTags.title : "") + '</a>');
                        }
                    }
                };
            } else {
                $('.q_list_bd').html('<li>暂时没有回答！</li>');
            }
        });
    };

    function twbsPaginations(res) {
        Page({
			num:Math.ceil(res / 5),					//页码数
			startnum:1,				//指定页码
			elem:$('#pagination'),		//指定的元素
			callback:function(page){	//回调函数
                if (page != 1) {
                    onoff = true;
                }
                if (onoff) {
                    $('.q_list_bd').html('');
                    generateList(page - 1);
                    $('html,body').animate({scrollTop:$('#content_warp ').offset().top-80}, 500);
                }
			}
		});
    };
});